import java.util.Arrays;

public class test {
    public static void main1(String[] args) {
        // 数组的创建及初始化 下列都是正确的方法
        int[] array = {1, 2, 3, 4};
        int[] array1 = new int[10];
        double[] array2 = new double[5];
        int[] array3 = new int[]{1, 23, 4, 5, 6};
        int[] array4;
        array4 = new int[10];
        int[] array5;
        array5 = new int[]{1, 2, 3, 45, 6};
        // int[] array6;
        // array6 = {1,2,3};这种是不对的
    }

    public static void main2(String[] args) {
        // 遍历数组
        int[] array1 = new int[]{1, 3, 4, 5, 6, 7};
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
        // 遍历数组
        int[] array = {1, 2, 3};
        for (int x : array) {
            System.out.println(x);
        }
    }

    public static void main3(String[] args) {
        // 认识null
        // int[] array1 = null;
        // System.out.println(array1.length);

    }

    public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4};

        fun2(array);
        for (int x : array) {
            System.out.println(x + "");
        }
    }

    public static void fun1(int[] array) {
        array[0] = 99;
    }

    public static void fun2(int[] array) {
        array = new int[]{11, 22, 33, 44, 55};
        // 这里是array这个引用指向了一个新的对象，是这个新的数组对象
    }

    // 数组作为函数的返回值
    // 数组返回斐波那契数列的前n项
    public static void main5(String[] args) {
        int[] array = fib(10);
        for (int x : array) {
            System.out.println(x + " ");
        }
    }

    public static int[] fib(int n) {
        if (n <= 0) {
            return null;
        }
        int[] array = new int[n];
        array[0] = array[1] = 1;
        for (int i = 2; i < n; i++) {
            array[i] = array[i - 2] + array[i - 1];
        }
        return array;
    }

    // 数组转字符串
    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4};
        String ret = Arrays.toString(array); // toString 方法
        System.out.println(ret);
    }

    // 自己实现toString 方法
    public static void main7(String[] args) {
        int[] array = {12, 100, 32, 109, 234};
        int[] array1 = null;
        int[] array2 = {};
        String ret = myString(array);
        String ret1 = myString(array1);
        String ret2 = myString(array2);
        System.out.println(ret);
        System.out.println(ret1);
        System.out.println(ret2);
    }

    public static String myString(int[] array) {
        if (array == null) {
            return "null";
        }
        if (array.length == 0) {
            return "[]";
        }
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if (i != array.length - 1) {
                // 最后一次不需要逗号
                ret += ", ";
            }
        }
        ret += "]";
        return ret;
    }


    // 实现冒泡排序
    public static void main8(String[] args) {
        int[] array = {12, 5, 6, 8, 29};
        bubbleSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void bubbleSort(int[] array) {
        if (array == null) {
            return;
        }
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flg = true;
                }
            }
            if (!flg) {
                return;
            }
        }
    }

    // 实现数组的逆序
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4};
        myReverse(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    public static void myReverse(int[] array) {
        if (array == null) {
            return;
        }
        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            int tmp = array[j];
            array[j] = array[i];
            array[i] = tmp;
            i++;
            j--;
        }
    }

}
